import 'package:common_utils/common_utils.dart';
import 'package:erp/main_state.dart';
import 'package:erp/res/colors.dart';
import 'package:erp/widgets/form1/form_builder.dart';
import 'package:erp/widgets/my_app_bar.dart';
import 'package:erp/widgets/my_button.dart';
import 'package:erp/widgets/process_attachment_upload.dart';
import 'package:erp/widgets/sale_project_detail_list.dart';
import 'package:erp/widgets/sale_project_title.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'controller.dart';

export 'binding.dart';

/// 立项申请
class SaleProjectApply extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var ms = Get.find<MainState>();
    var controller = Get.find<SaleProjectApplyController>();
    return Scaffold(
      backgroundColor: Colours.bg_color,
      appBar: MyAppBar(
        centerTitle: "立项申请",
        titleColor: Colors.black,
      ),
      body: SingleChildScrollView(
        controller: controller.scrollController,
        child: FormBuilder(
          initialValue: controller.formData,
          formManagement: controller.formManagement1,
          children: [
            SaleProjectTile(title: '市场立项项目信息', padding: EdgeInsets.only(left: 32.w)),
            _FormWrapper(
              child: Column(
                children: [
                  OptionSelectorFormField(
                    label: '* 所属公司',
                    name: 'company',
                    options: ms.getOptions('company'),
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "所属公司不能为空";
                      }
                    },
                  ),
                  ClearableTextFormField(
                    label: '* 项目名称',
                    name: 'projectName',
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "项目名称不能为空";
                      }
                    },
                  ),
                  ClearableTextFormField(
                    label: '* 立项编号',
                    name: 'projectNo',
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "立项编号不能为空";
                      }
                    },
                  ),
                  ClearableTextFormField(
                    label: '* 销售经理(申请人)',
                    name: 'applyEmp',
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "销售经理(申请人)不能为空";
                      }
                    },
                  ),
                  ClearableTextFormField(
                    label: '* 市场区域',
                    name: 'marketArea',
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "市场区域不能为空";
                      }
                    },
                  ),
                  OptionSelectorFormField(
                    label: '* 所属大区',
                    name: 'areaDept',
                    options: ms.getOptions('area_dept'),
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "所属大区不能为空";
                      }
                    },
                  ),
                  OptionSelectorFormField(
                    label: '* 投标状态',
                    name: 'bidStatus',
                    options: ms.getOptions('bid_status'),
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "投标状态不能为空";
                      }
                    },
                  ),
                ],
              ),
            ),
            SaleProjectTile(title: '项目基本情况', padding: EdgeInsets.only(left: 32.w)),
            _FormWrapper(
              child: Column(
                children: [
                  ClearableTextFormField(
                    label: '* 业主单位',
                    name: 'ownUnit',
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "业主单位不能为空";
                      }
                    },
                  ),
                  ClearableTextFormField(
                    label: '* 设计单位',
                    name: 'designUnit',
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "设计单位不能为空";
                      }
                    },
                  ),
                  ClearableTextFormField(
                    label: '* 总承包单位',
                    name: 'contractUnit',
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "总承包单位不能为空";
                      }
                    },
                  ),
                  ClearableTextFormField(
                    label: '* 招标单位',
                    name: 'bidUnit',
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "招标单位不能为空";
                      }
                    },
                  ),
                  ClearableTextFormField(
                    label: '* 建设地点',
                    name: 'buildAddress',
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "招建设地点不能为空";
                      }
                    },
                  ),
                  ClearableTextFormField(
                    label: '* 建设阶段',
                    name: 'buildStage',
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "建设阶段不能为空";
                      }
                    },
                  ),
                  DateSelectorFormField(
                    label: '* 启动时间',
                    name: 'beginTime',
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "启动时间不能为空";
                      }
                    },
                  ),
                  DateSelectorFormField(
                    label: '* 预竣工时间',
                    name: 'endTime',
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "预竣工时间不能为空";
                      }
                    },
                  ),
                  ClearableTextareaFormField(
                    label: '* 工期说明',
                    name: 'buildDesc',
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "工期说明不能为空";
                      }
                    },
                  ),
                  OptionSelectorFormField(
                    label: '* 建设方式',
                    name: 'buildType',
                    options: ms.getOptions('build_type'),
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "建设方式不能为空";
                      }
                    },
                  ),
                  OptionSelectorFormField(
                    label: '* 项目类别',
                    name: 'projectCategory',
                    options: ms.getOptions('project_category'),
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "项目类别不能为空";
                      }
                    },
                  ),
                  OptionSelectorFormField(
                    label: '* 主要负责技术部',
                    name: 'chargeDept',
                    options: ms.getOptions('charge_dept'),
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "主要负责技术部不能为空";
                      }
                    },
                  ),
                ],
              ),
            ),
            SaleProjectTile(title: '项目详细情况', padding: EdgeInsets.only(left: 32.w)),
            Container(
              color: Colors.white,
              padding: EdgeInsets.symmetric(horizontal: 34.w),
              child: SaleProjectDetailList(
                saleProjectDetailList: controller.saleProjectDetails,
                onEdit: (index) {
                  return controller.editProjectDetail(index);
                },
                onAdd: () {
                  return controller.addProjectDetail();
                },
              ),
            ),
            SaleProjectTile(title: '主要商务情况', padding: EdgeInsets.only(left: 32.w)),
            _FormWrapper(
              child: Column(
                children: [
                  OptionSelectorFormField(
                    label: '* 项目来源',
                    name: 'projectSource',
                    options: ms.getOptions('project_source'),
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "项目来源不能为空";
                      }
                    },
                  ),
                  OptionSelectorFormField(
                    label: '* 项目招投标方式',
                    name: 'bidMode',
                    options: ms.getOptions('bid_mode'),
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "项目招投标方式不能为空";
                      }
                    },
                  ),
                  FilterChipFormField(
                    items: controller.docListItems,
                    label: "* 需要材料",
                    name: 'docList',
                    validator: (value) {
                      if (ObjectUtil.isEmpty(value)) {
                        return "需要材料不能为空";
                      }
                    },
                  ),
                  ClearableTextFormField(
                    label: '其他材料',
                    name: 'otherDoc',
                  ),
                ],
              ),
            ),
            SaleProjectTile(title: '流程附件', padding: EdgeInsets.only(left: 32.w)),
            Container(
              padding: EdgeInsets.symmetric(horizontal: 52.w, vertical: 33.h),
              color: Colors.white,
              child: ProcessAttachmentUpload(
                fileList: controller.processFiles,
                onUpload: (FilePickerResult? result) {
                  return controller.uploadFiles(result);
                },
              ),
            ),
            SizedBox(height: 120.h),
            _Button(
              text: '保存为待办',
              onTap: () {
                controller.saveToBeTodo();
              },
            ),
            SizedBox(height: 40.h),
            _Button(
              text: '提交申请',
              onTap: () {
                controller.submit();
              },
            ),
            SizedBox(height: 140.h),
          ],
        ),
      ),
    );
  }
}

class _Button extends StatelessWidget {
  final Function? onTap;
  final String? text;

  _Button({Key? key, this.onTap, this.text});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 75.w),
      constraints: BoxConstraints(
        maxWidth: 600.w,
        maxHeight: 80.h,
      ),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(40.r)),
        gradient: LinearGradient(
          colors: [Color(0xFF3388FF), Color(0xFF66A6FF)],
          begin: AlignmentDirectional.centerEnd,
          end: AlignmentDirectional.centerStart,
        ),
      ),
      child: MyButton(
        minWidth: 600.w,
        minHeight: 80.h,
        textColor: Colors.white,
        fontSize: 34.sp,
        text: text ?? '',
        radius: 40.r,
        onPressed: () {
          if (onTap != null) {
            onTap!();
          }
        },
      ),
    );
  }
}

class _FormWrapper extends StatelessWidget {
  final Widget child;
  final Padding? padding;

  _FormWrapper({required this.child, this.padding});

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.symmetric(horizontal: 52.w),
      child: child,
    );
  }
}
